home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10724 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  79 lines

  1. Path: mail2news.demon.co.uk!fersys.demon.co.uk
  2. From: mlb@fersys.demon.co.uk
  3. Newsgroups: comp.lang.c
  4. Subject: Global variables in a multiple-sourcefile application
  5. Date: Tue, 19 Mar 1996 17:19:08 GMT
  6. Message-ID: <827255412.29892.0@fersys.demon.co.uk>
  7. X-NNTP-Posting-Host: fersys.demon.co.uk
  8. X-Broken-Date: Tue, 19 Mar 96 11:39 GMT
  9. Content-Type: text
  10. Content-Length: 1896
  11. X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!relay-4.mail.demon.net!post.demon.co.uk!fersys.demon.co.uk
  12.  
  13. I have two questions about the use of global variables in a 
  14. multiple-sourcefile application:
  15.  
  16. 1) If I have a global variable x that is used in both source files of
  17. an application, but I don't need it initialised in its definition,
  18. I would generally put "int x;" in one and "extern int x;" in the other.
  19.  
  20. However, is there anything unsafe about putting a declaration in a header 
  21. file included by both files, without a definition in either file, or
  22. do I really need a definition in one of the files?
  23.  
  24. i.e. is this OK?:
  25.  
  26.                  common.h
  27.                  ========
  28.              int x;
  29.  
  30.  
  31.     a.c                b.c
  32.     ===                ===
  33.     #include "common.h"        #include "common.h"
  34.  
  35.     main ()                some_func()
  36.     {                {
  37.         x = 3;                int y;
  38.  
  39.         ...                y = x;
  40.     }                    ...
  41.                     }
  42.  
  43. 2) Following on from q1, what happens if two source-files each contain
  44. a global variable declaration for the same variable name, but each has
  45. a different type (by accident or possibly deliberately)?  
  46. The programs appear to compile, and behave as though
  47. the variable is cast appropriately.  
  48. The example below should clarify what I mean (prototypes etc omitted):
  49.  
  50.     a.c                b.c
  51.     ===                ===
  52.  
  53.     int x;                short x;
  54.  
  55.     main()                f()
  56.     {                {
  57.         x = 3;                printf ("x=%d\n", x);
  58.         f();                x = 1;
  59.         printf("x=%d\n", x);    }
  60.     }
  61.  
  62. The exact behaviour appears to depend on the endian-ness of the machine,
  63. size of short and int etc, e.g. on my machine I get the following output:
  64.   x=0
  65.   x=65539
  66.  
  67. but the same variable appears to be accessed, but 
  68. a.c treats it as an int, and b.c as a short.  This appears dangerous.
  69.  
  70. Does anyone have any light to shed on the above issues.  (I have an old
  71. posting of C.Torek's that discussed "tentative" declarations briefly,
  72. but it didn't address the subject fully).
  73.  
  74. TIA
  75.  
  76. Mark Bergman                       Email: mlb@fersys.co.uk
  77. Ferranti Syseca Ltd.               Tel:   0161-946 7178
  78. Wythenshawe, Manchester, UK        Fax:   0161-946 7001
  79.